package com.thc.servicetest.controller;

import com.thc.servicetest.persistence.beans.User;
import com.thc.servicetest.persistence.beans.UserExample;
import com.thc.servicetest.persistence.mapper.UserMapper;
import com.thc.servicetest.util.RedisUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author lzz
 * @package com.thc.servicetest.controler
 * @create 2019-05-15 10:01
 * @description: ${DESCRIPTION}
 */
@RestController
public class HelloControler {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/hi")
    public String hi(){
        return port+ "11";
    }

    @RequestMapping("/selectByID")
    public JSONObject selectByID(@RequestBody JSONObject json){
        UserExample userExample = new UserExample();
        userExample.or().andIdEqualTo(json.getString("id"));
        List<User> users = userMapper.selectByExample(userExample);
        return JSONObject.fromObject(users.get(0));
    }

    @RequestMapping("/selectAll")
    public JSONArray selectAll(){
        UserExample userExample =new UserExample();
        userExample.createCriteria();
        return  JSONArray.fromObject(userMapper.selectByExample(userExample).toArray());
    }

}
